package com.atguigu.ggkt.vod.service.impl;

import com.atguigu.ggkt.model.vod.Video;
import com.atguigu.ggkt.vod.mapper.VideoMapper;
import com.atguigu.ggkt.vod.service.VideoService;
import com.atguigu.ggkt.vod.service.VodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author lxx
 * @since 2022-06-30
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Autowired
    private VodService vodService;

    @Override
    public void removeVideoAndVod(Long id) {
        Video video = getById(id);
        String fileId = video.getVideoSourceId();
        // 1.如果小节包含视频，则视频一同删除
        if (!StringUtils.isEmpty(fileId)) {
            vodService.deleteVideo(fileId);
        }
        // 2.删除小节
        removeById(id);
    }

    @Override
    public void deleteVideoByCourseId(Long id) {
        List<Video> videoList = query().eq("course_id", id).list();
        // 1.如果小节列表为空，直接退出
        if (videoList.isEmpty()) {
            return;
        }
        videoList.forEach(video -> {
           // 2.删除小节
           removeVideoAndVod(video.getId());
        });
    }
}
